package com.rpframework.module.common.bottom.weixin.util;

import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;

import com.rpframework.core.json.EmptyException;
import com.rpframework.module.common.bottom.service.impl.BaseServiceImpl;

/**
 * 配置文件读写工具类
 * 
 * @author wx
 * @version 1.0
 */
public class Configuration {

	/**
	 * 读取配置文件信息
	 * 
	 * @param name
	 *            读取节点名
	 * @param fileName
	 *            文件名
	 * @return 读取的节点值
	 */
	public static String readConfigString(String name, String fileName) {
		String result = null;
		fileName = "properties/" + fileName;
		try {
			InputStream resourceAsStream = BaseServiceImpl.class.getClassLoader()
					.getResourceAsStream(fileName);
			Properties properties = new Properties();
			properties.load(resourceAsStream);
			result=properties.getProperty(name);
		} catch (Exception e) {
			throw new EmptyException("从" + fileName + "读取" + name + "出错:", e);
		}
		return result;
	}

	/**
	 * 读取配置文件信息
	 * 
	 * @param name
	 *            读取节点名
	 * @param fileName
	 *            文件名
	 * @return 读取的节点值
	 */
	public static int readConfigInteger(String name, String fileName) {
		int result = 0;
		fileName = "properties/" + fileName;
		try {
			ResourceBundle rb = ResourceBundle.getBundle(fileName);
			result = Integer.parseInt(rb.getString(name));
		} catch (Exception e) {
			throw new EmptyException("从" + fileName + "读取" + name + "出错:" + e.getMessage());
		}
		return result;
	}

	public static long readConfigLong(String name, String fileName) {
		long result = 0;
		fileName = "properties/" + fileName;
		try {
			ResourceBundle rb = ResourceBundle.getBundle(fileName);
			result = Long.parseLong(rb.getString(name));
		} catch (Exception e) {
			throw new EmptyException("从" + fileName + "读取" + name + "出错:" + e.getMessage());
		}
		return result;
	}

	public static double readConfigDouble(String name, String fileName) {
		double result = 0;
		fileName = "properties/" + fileName;
		try {
			ResourceBundle rb = ResourceBundle.getBundle(fileName);
			result = Double.parseDouble(rb.getString(name));
		} catch (Exception e) {
			throw new EmptyException("从" + fileName + "读取" + name + "出错:" + e.getMessage());
		}
		return result;
	}
}
